package lines.gameengine;

import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

/**
 * @author vpotapenko
 */
public class CellsHolderTest {

    private static final int WIDTH = 9;
    private static final int HEIGHT = 10;
    private static final int VALID_X = 5;
    private static final int VALID_Y = 8;
    private static final int INVALID_X = -1;
    private static final int INVALID_Y = 17;

    private CellsHolder cellsHolder;

    @Before public void setUp() throws Exception {
        cellsHolder = new CellsHolder();

        cellsHolder.initialize(WIDTH, HEIGHT);
    }

    @Test public void shouldPrepareCells() {
        assertThat(cellsHolder.getWidth(), equalTo(WIDTH));
        assertThat(cellsHolder.getHeight(), equalTo(HEIGHT));
        assertThat(cellsHolder.getCell(VALID_X, VALID_Y), notNullValue());
    }

    @Test public void shouldReturnNullIfInvalidPoint() {
        assertThat(cellsHolder.getCell(INVALID_X, INVALID_Y), nullValue());

        FieldPoint point = new FieldPoint(INVALID_X, INVALID_Y);
        assertThat(cellsHolder.getCell(point), nullValue());
    }

    @Test public void shouldClearAllBallsOnReset() {
        Cell cell = cellsHolder.getCell(VALID_X, VALID_Y);
        cell.setBall(new Ball(null));
        assertThat(cell.getBall(), notNullValue());

        cellsHolder.reset(WIDTH, HEIGHT);
        cell = cellsHolder.getCell(VALID_X, VALID_Y);
        assertThat(cell.getBall(), nullValue());
    }

    @Test public void shouldReinitializeIfAnotherSizeOnReset() {
        cellsHolder.reset(WIDTH + 1, HEIGHT);

        assertThat(cellsHolder.getWidth(), equalTo(WIDTH + 1));
        assertThat(cellsHolder.getHeight(), equalTo(HEIGHT));
    }
}
